import { queryTrashApi, deleteTrashApi } from "../service";
import { XS_SIZE } from "@/generic/ConfigParams";

const Trash = {
    namespace: 'trash',
    state: {
        currentUser: {},
        data: [],
        isMobile: false,
    },
    effects: {
        *fetchTable(params, { call, put, select }) {
            const args = {}
            if (params.payload && params.payload.fileid) {
                // 查询单个文件的明细
                args = { fileid: params.payload.fileid}
            }
            // console.log("==>> args", args)
            const response = yield call(queryTrashApi, args, true);
            if (!(response instanceof Response)) {
                yield put({
                    type: "UpdateData",
                    payload: response.results
                })
                return true;
            }

            return false;
        },

        *deleteTrash(params, { call, put, select }) {
            console.log("==>>> deleteTrash is run !")
            if (params.payload && params.payload.fileid) {
                // 删除文件的情况
            }
            else{
                // 清空回收站
                const response = yield call(deleteTrashApi)
                if (response.ok) {
                    yield put({
                        type: 'UpdateData',
                        payload: []
                    })
                    return true;
                }

                return false;
            }
        },
    },
    reducers: {
        UpdateData(state, action) {
            // console.log("===>> action", action.payload)
            return { ...state, data: action.payload || []}
        },

        UpdateIsMobile(state, action) {
            // console.log("===>> UpdateIsMobile", action.payload)
            return { ...state, isMobile: action.payload.size < XS_SIZE ? true : false }
        },
    },
}

export default Trash;